<template>
    <div class="login_container">
        <div class="login_box">
            <!-- 登录logo区域 -->
            <div class="avator_box">
                <div class="wave"></div>
            </div>
            <!-- 登录表单区域 -->
            <el-form class="form_wrapper" ref="loginFormRef" :model="loginForm" :rules="loginFormRules">
                <el-form-item prop="username">
                    <el-input v-model="loginForm.username" name="username" placeholder="Username"></el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input v-model="loginForm.password" type="password" name="password" placeholder="Password"></el-input>
                </el-form-item>
                <input type="button" class="btn" value="Login" @click="login">
                <!--<div class="btn">Login</div>-->
                <div class="msg">
                    Don't have account? <a href="#">Sign up</a>
                </div>
            </el-form>
        </div>
    </div>
</template>

<script>
export default {
  data () {
    return {
      // 登陆表单的数据绑定对象
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      // 表单的验证规则
      loginFormRules: {
        // 用户名验证
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在3到10个字符之间', trigger: 'blur' }
        ],
        // 密码验证
        password: [
          { required: true, message: '请输入登录密码', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在6到15个字符之间', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    login () {
      this.$refs.loginFormRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.post('login', this.loginForm)
        if (res.meta.status !== 200) return this.$message.error('登录失败')
        this.$message.success('登录成功')
        // 将登录成功后的token保存到客户端的sessionStorage中
        // 因为项目中除了登录之外的其他API接口只能在登录后才能访问，客户端保存的token在网站登录成功后才能生效，所以需要保存在sessionStorage中
        // console.log(res)
        window.sessionStorage.setItem('token', res.data.token)
        await this.$router.push('/home')
      })
    }
  }
}
</script>

<style lang="less" scoped>
    .login_container {
        /*background: linear-gradient(to right, #373b44, #3b8d99);*/
        background: linear-gradient(to right, #373b44, #44a08d);
        height: 100%;
    }

    .login_box {
        width: 350px;
        height: 400px;
        background-color: #fff;
        border-radius: 5px;
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);

        .avator_box, .wave {
            height: 100px;
            width: 100px;
            border-radius: 50%;
            position: absolute;
            left: 50%;
        }

        .avator_box {
            border: 1px solid #fff;
            padding: 5px;
            transform: translate(-50%, -50%);
            background-color: #fff;
            box-shadow: 0 0 10px #faf8ff;
        }

        .wave {
            /*background-color: #9bd7ff;*/
            background: linear-gradient(to left, #86a8e7, #91eae4);
            overflow: hidden;
            transform: translate(-50%)
        }

        .wave:after {
            content: '';
            width: 130px;
            height: 130px;
            background-color: rgba(255, 255, 255, 0.8);
            position: absolute;
            left: 50%;
            top: 0;
            transform: translate(-50%, -55%);
            border-radius: 40%;
            animation: wave 5s linear infinite;
        }

        .wave::before {
            content: 'RX';
            position: absolute;
            left: 50%;
            top: 0;
            background-image: -webkit-linear-gradient(45deg, #86a8e7, #ee9ca7);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            font-size: 20px;
            z-index: 99;
            transform: translate(-50%, 25px)
        }

        @keyframes wave {
            100% {
                transform: translate(-50%, -55%) rotate(360deg);
            }
        }

        .form_wrapper {
            width: 100%;
            height: 250px;
            position: absolute;
            bottom: 5px;
            display: block;
            box-sizing: border-box;
            padding: 0 30px;

            /*.input_item, el-form-item {*/
            /*    display: block;*/
            /*    width: 100%;*/
            /*    margin-bottom: 20px;*/
            /*    border: 0;*/
            /*    padding: 5px;*/
            /*    border-bottom: 1px solid #a4a3a8;*/
            /*    font-size: 15px;*/
            /*    !*text-transform: uppercase;*!*/
            /*    outline: none;*/
            /*    box-sizing: border-box;*/
            /*}*/

            .btn {
                display: block;
                width: 100%;
                height: 30px;
                text-align: center;
                padding: 2px;
                margin-top: 50px;
                background: linear-gradient(to left, #86a8e7, #91eae4);
                border: 0;
                border-radius: 2px;
                box-shadow: 0 0 5px #c9cfcd;
                color: #fff;
                font-size: 15px;
                box-sizing: border-box;
                outline: none;
                cursor: pointer;
            }

            .msg {
                text-align: center;
                line-height: 40px;
                box-sizing: border-box;
                font-size: 12px;

                a {
                    text-decoration: none;
                    color: #86a8e7;
                }
            }
        }
    }

</style>
